package com.hospital.auth.controller;

import com.hospital.auth.service.AuthService;
import com.hospital.common.domain.response.JwtResponse;
import com.hospital.common.domain.system.UserInfo;
import com.hospital.common.exceptions.ServiceException;
import com.hospital.common.result.R;
import com.hospital.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author：sjc
 * @Description:
 * @name：AuthController
 * @Date：2024/10/18 19:36
 */
@RestController
@RequestMapping("auth")
public class AuthController {

    @Autowired
    private AuthService authService;

    /**
     * 用户登录
     * @param patientVo
     * @return
     */
    @PostMapping("login")
    public R login(@RequestBody UserInfo patientVo) {

        //判断账号密码是否为空
        if(StringUtils.isAllBlank(patientVo.getAccount(), patientVo.getPassword())){
            throw new ServiceException("账号或密码为空！",401);
        }
        JwtResponse jwtResponse = authService.login(patientVo);
        return R.success(jwtResponse);

    }

    /**
     * 获取用户登录信息
     * @return
     */
    @GetMapping("getInfo")
    public R getInfo() {
        UserInfo info = authService.getInfo();
        return R.success(info);
    }

    /**
     * 用户注册
     */
    @PostMapping("register")
    public R register(@RequestBody @Validated UserInfo patientVo) {
        //判断账号密码是否为空
//        if(StringUtils.isAllBlank(patientVo.getAccount(), patientVo.getPassword())){
//            throw new ServiceException("账号或密码为空！",401);
//        }
        if(!patientVo.getCardId().matches("^[1-9]\\d{5}(?:18|19|20)\\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\\d|30|31)\\d{3}[\\dXx]$")){
            throw new ServiceException("身份证格式错误！", 401);
        }
        if(!patientVo.getPhone().matches("^(?:(?:\\+|00)86)?1\\d{10}$")){
            throw new ServiceException("手机号格式错误！", 401);
        }
        authService.register(patientVo);
        return R.success("ok了");
    }



}
